function bleScan() {
    var result = "";
    bluetoothSerial.list(function(device){
        console.log("device:"+device);
        for(var i=0; i<device.length; i++) {
            if(result.indexOf(device[i].id) < 0) {
                result += device[i].name;
                result += device[i].id;
                result += "<br />";
                document.getElementById("bleScanInfo").innerHTML = result;
            }
        }
    }, function(){
        document.getElementById("bleScanInfo").innerHTML = "扫描失败";
    });
}

function stopUnpaired() {
    bluetoothSerial.stopDiscoverUnpaired(null, null);
    document.getElementById("stopUnpairedInfo").innerHTML = "停止扫描";
}

function bleConnect() {
    var uuid = "F6:AC:52:47:E8:72";//测试使用的，实际以开发者的连接的蓝牙为准
    bluetoothSerial.connect(uuid, function(){
        document.getElementById("connectBleInfo").innerHTML = "连接成功";
    }, function(){
        document.getElementById("connectBleInfo").innerHTML = "连接失败";
    });
}

function gbkStringToUint8Array(gbkStr) {
    // GBK.encode返回一个数组
    const gbkArray = GBK.encode(gbkStr);
    // 将数组转换为Uint8Array
    return new Uint8Array(gbkArray);
}


function bleWrite() {
    var cmd = new _EscCommand(true);
    var content = cmd.TextAlignLeft + "打印测试";
    content += "\n\n\n\n";
    content += cmd.CutAllPage;
    var uint8Array = gbkStringToUint8Array(content);
    bluetoothSerial.write(uint8Array, function(){
        document.getElementById("writeBleInfo").innerHTML = "打印成功";
    }, function(){
        document.getElementById("writeBleInfo").innerHTML = "打印失败";
    });

}

function bleDisConnect() {
    bluetoothSerial.disconnect(function(){
        document.getElementById("disconnectBleInfo").innerHTML = "关闭成功";
    }, function(){
        document.getElementById("disconnectBleInfo").innerHTML = "关闭失败";
    });
}